
    /**  
    * @Title: SocketIOAuthController.java
    * @Package com.efast.cafe.portal.controller.auth
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年10月31日
    * @version V1.0  
    */
    
package com.efast.cafe.portal.controller.auth;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.FrameConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.mail.MailSenderService;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.common.PortalUserSecurityBean;
import com.efast.cafe.portal.bean.mail.SampleMailBean;
import com.efast.cafe.portal.entity.common.PortalSocketUser;
import com.efast.cafe.portal.service.common.IPortalSocketUserService;
import com.efast.cafe.util.ConfigUtil;

/**
    * @ClassName: SocketIOAuthController
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年10月31日
    *
    */
@Controller
public class SocketIOAuthController {
	@Autowired
	private IPortalSocketUserService socketUserService;
	
	@Autowired
	private StringRedisTemplate redisTemplate;
	

	/**
	 * 
	    * @Title: authLogin
	    * @Description: 外部设备连接socketio服务前 获取token
	    * @param @param uid
	    * @param @param pwd
	    * @param @return    参数
	    * @return ResultBean    返回类型
	    * @throws
	 */
	@RequestMapping(value ="/openSocket/auth/authorize",method= RequestMethod.POST)
	public @ResponseBody ResultBean authLogin(@RequestParam String uid,
			@RequestParam String pwd,HttpSession session){
		ResultBean res = new ResultBean(false);
		
		try{
			PortalSocketUser user = socketUserService.authorize(uid, pwd);
			if(null!=user){
				String token = UUID.randomUUID().toString();
				JSONObject jo = new JSONObject();
				jo.put("token", token);
				jo.put("serverPath", ConfigUtil.getValue("socketio.url"));
				res.setObject(jo);
				res.setSuccess(true);
				redisTemplate.opsForValue().set(FrameConstant.SOCKET_IO_TEMP_TOKEN+token, uid, 120, TimeUnit.SECONDS);
			}else{
				res.setErrorCode("FAILURE");
				res.setMessage("uid or pwd error.");
			}
			session.invalidate();
		}catch(Exception e){
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	/**
	 * 
	    * @Title: webAuthLogin
	    * @Description: 已登录平台用户连接长连接前请求token
	    * @param @return    参数
	    * @return ResultBean    返回类型
	    * @throws
	 */
	@RequestMapping(value ="/socket/auth/authorize",method= RequestMethod.POST)
	public @ResponseBody ResultBean webAuthLogin(){
		ResultBean res = new ResultBean(false);
		try{
			PortalUserSecurityBean user = SpringWebUtils.getRequestUserObj();
			if(null!=user){
				String token = UUID.randomUUID().toString();
				JSONObject jo = new JSONObject();
				jo.put("token", token);
				jo.put("serverPath", ConfigUtil.getValue("socketio.url"));
				res.setObject(jo);
				res.setSuccess(true);
				redisTemplate.opsForValue().set(FrameConstant.SOCKET_IO_TEMP_TOKEN+token, user.getUsername(), 120, TimeUnit.SECONDS);
			}else{
				res.setErrorCode("FAILURE");
				res.setMessage("No session found");
			}
		}catch(Exception e){
			res.setErrorCode("SYSTEM_ERROR");
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
//	@Autowired
//	private MailSenderService mailSenderService;
//	
//	@RequestMapping(value ="/openSocket/mail",method= RequestMethod.GET)
//	public @ResponseBody ResultBean mailTest(){
//		ResultBean r = new ResultBean(true,"");
//		try{
//			List<SampleMailBean> mailList = new ArrayList<>();
//			SampleMailBean mail = new SampleMailBean();
//			mail.setRetCode("200");
//			mail.setRetRemark("处理成功");
//			mailList.add(mail);
//			
//			SampleMailBean mail1 = new SampleMailBean();
//			mail1.setRetCode("404");
//			mail1.setRetRemark("未找到接口");
//			mailList.add(mail1);
//			mailSenderService.sendMessageMail("我是带模板和附件的测试邮件", "sampleMailTemplate.ftl", mailList, "360808987@qq.com",new File("E:\\CAFE资料\\CAFE目录结构.xmind"));
//			//mailSenderService.sendMessageMail("我是测试邮件", "我是测试内容", "360808987@qq.com",new File("E:\\CAFE资料\\CAFE目录结构.xmind"));
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		return r;
//	}
	
}
